/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-10-09
*/
package com.rzico.account.service;

import com.alibaba.fastjson.JSON;
import com.rzico.account.entity.Payment;
import com.rzico.account.entity.Refunds;
import com.rzico.account.mapper.DepositMapper;
import com.rzico.account.mapper.MatchPointMapper;
import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.account.entity.MatchPoint;
import com.rzico.basics.entity.Member;
import com.rzico.basics.mapper.MemberMapper;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MsgService;
import com.rzico.core.service.SysMchService;
import com.rzico.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 * 积分账单业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class MatchPointService extends BaseServiceImpl<MatchPoint, String> {

    @Autowired
    private MatchPointMapper matchPointMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private RefundsService refundsService;

    @Autowired
    private PaymentService paymentService;

    @Autowired @Lazy
    private MsgService msgService;

    @Override
    public BaseMapper<MatchPoint, String> getMapper() {
        return matchPointMapper;
    }

    //付款
    @Transactional(rollbackFor = Exception.class)
    public void payment(Payment payment) {

        if (!payment.getStatus().equals(Payment.STATUS_WAITING)) {
            throw new CustomException("不是待付款状态");
        }

        payment.setStatus(Payment.STATUS_SUCCESS);
        payment.setPaymentDate(new Date());
        payment.setPaymentPluginId("matchPointPayPlugin");
        payment.setPaymentPluginName("积分");
        paymentService.updateByPrimaryKeySelective(payment);

        Long value = payment.getAmount().multiply(new BigDecimal(100)).setScale(0).longValue();

        Member member = memberMapper.selectByPrimaryKey(payment.getMemberId());
        if (member.getMatchPoint()<value) {
            throw new CustomException("可用积分不足");
        }
        Map<String,Object> updateParams = new HashMap<>();
        updateParams.put("id",payment.getMemberId());
        updateParams.put("value",value);
        memberMapper.decMatchPoint(updateParams);

        //更新重新读取余额
        member = memberMapper.selectByPrimaryKey(payment.getMemberId());
        if (member.getPoint()<0L) {
            throw new CustomException("可用积分不足");
        }

        MatchPoint matchPoint = new MatchPoint();
        matchPoint.setBalance(member.getMatchPoint());
        matchPoint.setDebit(value);
        matchPoint.setCredit(0L);
        matchPoint.setCreateDate(new Date());
        matchPoint.setDeleted(false);
        matchPoint.setEnterpriseId(payment.getEnterpriseId());
        matchPoint.setMemberId(payment.getMemberId());
        matchPoint.setMemo(payment.getMemo());
        matchPoint.setPaymentId(payment.getId());
        matchPoint.setType(1);
        matchPoint.setDeleted(false);
        matchPoint.setOrderId(payment.getOrderId());
        matchPoint.setOrderType(payment.getOrderType());
        super.insertUseGeneratedKeys(matchPoint);

        Map<String,Object> content = new HashMap<>();

        content.put("title", "付款成功-积分");

        content.put("memo",payment.getMemo());
        content.put("amount",payment.getAmount());
        content.put("balance",matchPoint.getBalance());
        content.put("type",3);
        msgService.sendNotity(member,"msg-account",content.get("title").toString(), JSON.toJSONString(content),null);


    }

    //退款
    @Transactional(rollbackFor = Exception.class)
    public void refunds(Refunds refunds) {

        if (!refunds.getStatus().equals(Refunds.STATUS_WAITING)) {
            throw new CustomException("不是待退款状态");
        }
        refunds.setStatus(Refunds.STATUS_REFUNDED);
        refunds.setRefundsDate(new Date());
        refunds.setPaymentPluginId("matchPointPayPlugin");
        refunds.setPaymentPluginName("积分");
        refundsService.updateByPrimaryKeySelective(refunds);

        Long value = refunds.getAmount().multiply(new BigDecimal(100)).setScale(0).longValue();

        Map<String,Object> updateParams = new HashMap<>();
        updateParams.put("id",refunds.getMemberId());
        updateParams.put("value",value);
        memberMapper.addMatchPoint(updateParams);

        //更新重新读取余额
        Member member =  memberMapper.selectByPrimaryKey(refunds.getMemberId());

        MatchPoint matchPoint = new MatchPoint();
        matchPoint.setBalance(member.getMatchPoint());
        matchPoint.setDebit(0L);
        matchPoint.setCredit(value);
        matchPoint.setCreateDate(new Date());
        matchPoint.setDeleted(false);
        matchPoint.setEnterpriseId(refunds.getEnterpriseId());
        matchPoint.setMemberId(refunds.getMemberId());
        matchPoint.setMemo(refunds.getMemo());
        matchPoint.setPaymentId(refunds.getId());
        matchPoint.setType(2);
        matchPoint.setDeleted(false);
        matchPoint.setOrderId(refunds.getOrderId());
        matchPoint.setOrderType(refunds.getOrderType());
        super.insertUseGeneratedKeys(matchPoint);

        Map<String,Object> content = new HashMap<>();

        content.put("title", "退款成功-积分");

        content.put("memo",refunds.getMemo());
        content.put("amount",refunds.getAmount());
        content.put("balance",matchPoint.getBalance());
        content.put("type",3);
        msgService.sendNotity(member,"msg-account",content.get("title").toString(), JSON.toJSONString(content),null);


    }

    //收入
    @Transactional(rollbackFor = Exception.class)
    public void addMatchPoint(Long memberId,Long value,String memo,Integer orderType,Long orderId) {

        Member member = memberMapper.selectByPrimaryKey(memberId);

        Map<String,Object> updateParams = new HashMap<>();
        updateParams.put("id",memberId);
        updateParams.put("value",value);
        memberMapper.addMatchPoint(updateParams);

        //更新重新读取余额
        member = memberMapper.selectByPrimaryKey(memberId);

        MatchPoint matchPoint = new MatchPoint();
        matchPoint.setBalance(member.getMatchPoint());
        matchPoint.setDebit(0L);
        matchPoint.setCredit(value);
        matchPoint.setCreateDate(new Date());
        matchPoint.setDeleted(false);
        matchPoint.setEnterpriseId(member.getEnterpriseId());
        matchPoint.setMemberId(memberId);
        matchPoint.setMemo(memo);
        matchPoint.setType(0);
        matchPoint.setDeleted(false);
        matchPoint.setOrderType(orderType);
        matchPoint.setOrderId(orderId);
        super.insertUseGeneratedKeys(matchPoint);

    }

    //支出
    @Transactional(rollbackFor = Exception.class)
    public void decMatchPoint(Long memberId,Long value,String memo,Integer orderType,Long orderId) {

        Member member = memberMapper.selectByPrimaryKey(memberId);

        if (member.getMatchPoint()<value) {
            throw new CustomException("积分余额不足");
        }

        Map<String,Object> updateParams = new HashMap<>();
        updateParams.put("id",memberId);
        updateParams.put("value",value);
        memberMapper.decMatchPoint(updateParams);

        //更新重新读取余额
        member = memberMapper.selectByPrimaryKey(memberId);

        if (member.getMatchPoint()<0) {
            throw new CustomException("积分余额不足");
        }

        MatchPoint matchPoint = new MatchPoint();
        matchPoint.setBalance(member.getMatchPoint());
        matchPoint.setDebit(0L);
        matchPoint.setCredit(0L-value);
        matchPoint.setCreateDate(new Date());
        matchPoint.setDeleted(false);
        matchPoint.setEnterpriseId(member.getEnterpriseId());
        matchPoint.setMemberId(memberId);
        matchPoint.setMemo(memo);
        matchPoint.setType(0);
        matchPoint.setDeleted(false);
        matchPoint.setOrderType(orderType);
        matchPoint.setOrderId(orderId);
        super.insertUseGeneratedKeys(matchPoint);

    }
}
